﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Data.SqlClient;

public partial class reco_course : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String sid = Session["mid"].ToString();
        int tag = int.Parse(LabelTag.Text.ToString());
        if (!IsPostBack || tag == 0)
        {
            CoursePresent(tag, sid);
            SetTop(0);
        }
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        ds = db.GetDataSet("select * from mr_v_comment where time > (select last_comment_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentComment.Text = "最新评论(" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentComment.NavigateUrl = "~/recentcomment.aspx";
        HyperLinkRecentComment.ForeColor = Color.Blue;
        ds = db.GetDataSet("select * from mr_v_download where time > (select last_download_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentDownload.Text = "最新下载(" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentDownload.NavigateUrl = "~/recentdownload.aspx";
        HyperLinkRecentDownload.ForeColor = Color.Blue;

        string lasttime;
        SqlDataReader last_logn_time = db.GetReader("select last_resource_time from mr_member where mid = '" + Session["mid"].ToString() + "'");
        if (last_logn_time.Read())
        {
            lasttime = last_logn_time.GetValue(0).ToString();
            SqlDataReader count = db.GetReader("select count(*) from mr_resource where upload_time >'" + lasttime + "'");
            if (count.Read())
            {
                HyperLink24.Text = "最新资源（" + count.GetValue(0).ToString() + "）";
            }
            else
                HyperLink24.Text = "最新资源（0）";
        }

    }
    protected void ButtonRecommand_Click(object sender, EventArgs e)
    {
        String sid = Session["mid"].ToString();
        LabelPresent.Text = "推荐";
        LabelTag.Text = "1";
        CoursePresent(1,sid);
    }

    protected void ButtonNoReco_Click(object sender, EventArgs e)
    {
        String sid = Session["mid"].ToString();
        LabelPresent.Text = "已评";
        LabelTag.Text = "2";
        CoursePresent(2, sid);

    }
    protected void ButtonAll_Click(object sender, EventArgs e)
    {
        String sid = Session["mid"].ToString();
        LabelPresent.Text = "全部";
        LabelTag.Text = "0";
        CoursePresent(0,sid);
    }

    public void CoursePresent(int tag, String getSid)
    {
        Panel1.Controls.Clear();
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();
        ds1 = db.GetDataSet("select * from mr_parameter where pname = 'sim'");
        double sim = double.Parse(ds1.Tables[0].Rows[0]["value"].ToString());
        int i;
        int sid = int.Parse(getSid);
        MyVector mv = new MyVector();
        MyVector mvall = new MyVector(sid);
        MyVector mvreco = new MyVector();
        MyVector mvnoreco = new MyVector();
        mvall.Recommand(sim);
        mvall.Sort();
        mvreco = mvall.GetRecommand();
        mvnoreco = mvall.GetNoRecommand();
        if (tag == 0)
            mv = mvall;
        else if (tag == 1)
            mv = mvreco;
        else
            mv = mvnoreco;
        for (i = 0; i < mv.GetLength(); i++)
        {
            ds = db.GetDataSet("select * from mr_v_course where cname = '" + mv.GetIname(i) + "'");
            String cid = ds.Tables[0].Rows[0]["cid"].ToString();
            ds2 = db.GetDataSet("select count(*) from mr_resource where cid = '" + cid + "'");
            String resourceCount = ds2.Tables[0].Rows[0][0].ToString();
            con_coursedetail cd = (con_coursedetail)Page.LoadControl("~/Controls/con_coursedetail.ascx");
            String cname = ds.Tables[0].Rows[0]["cname"].ToString();
            String logo = ds.Tables[0].Rows[0]["logo"].ToString();
            String department = ds.Tables[0].Rows[0]["dname"].ToString();
            String type;
            if (mv.GetTag(i) == 0)
                type = "已评分";
            else
                type = "推荐";
            String recommand = mv.GetValue(i).ToString("0.00") + "(" + type + ")";
            String keyword = ds.Tables[0].Rows[0]["keyword"].ToString();
            String introduction = ds.Tables[0].Rows[0]["cintroduction"].ToString();
            cd.SetCourse(cid, logo, cname, department, recommand, resourceCount, keyword, introduction);
            Panel1.Controls.Add(cd);
        }
        if (mv.GetLength() == 0) LabelNoRows.Visible = true;
    }

    public void SetTop(int tag)
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        String tablename = "mr_test";
        String condition = "";
        DateTime currentTime = new DateTime();
        currentTime = DateTime.Now;
        if (tag == 0)
        {
            tablename = "mr_v_downloadcount";
            LabelTitle.Text = "总排行榜";
        }
        else if (tag == 1)
        {
            tablename = "mr_v_downloadcount_month ";
            condition = "where year = '" + currentTime.Year.ToString() + "' and month = '" + (currentTime.Month - 1).ToString() + "'";
            LabelTitle.Text = "月排行榜";
        }
        else if (tag == 2)
        {
            tablename = "mr_v_downloadcount_year ";
            condition = "where year = '" + currentTime.Year.ToString() + "'";
            LabelTitle.Text = "年排行榜";
        }
        ds = db.GetDataSet("select top 5 * from " + tablename + condition + "  order by num desc");
        HyperLinkRank1.Text = ds.Tables[0].Rows[0]["rname"].ToString();
        HyperLinkRank1.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount1.Text = ds.Tables[0].Rows[0]["num"].ToString();
        HyperLinkRank2.Text = ds.Tables[0].Rows[1]["rname"].ToString();
        HyperLinkRank2.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount2.Text = ds.Tables[0].Rows[1]["num"].ToString();
        HyperLinkRank3.Text = ds.Tables[0].Rows[2]["rname"].ToString();
        HyperLinkRank3.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount3.Text = ds.Tables[0].Rows[2]["num"].ToString();
        HyperLinkRank4.Text = ds.Tables[0].Rows[3]["rname"].ToString();
        HyperLinkRank4.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount4.Text = ds.Tables[0].Rows[3]["num"].ToString();
        HyperLinkRank5.Text = ds.Tables[0].Rows[4]["rname"].ToString();
        HyperLinkRank5.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount5.Text = ds.Tables[0].Rows[4]["num"].ToString();
    }
    protected void ButtonMonth_Click(object sender, EventArgs e)
    {
        SetTop(1);
    }
    protected void ButtonYear_Click(object sender, EventArgs e)
    {
        SetTop(2);
    }
    protected void ButtonAll_Click1(object sender, EventArgs e)
    {
        SetTop(0);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox2.Text;
        Response.Redirect("./Student/Search.aspx");
    }
}